package com.seven.alert.properties;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.seven.alert.exception.SmsException;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.HashMap;

@Data
@ConfigurationProperties(
        prefix = "alert.sms.ali"
)
@Log4j2
public class Ali implements BaseSms {
    private String regionId;
    private String accessKeyId;
    private String secret;
    private String signName;
    private String templateCode;
    private String sysDomain = "dysmsapi.aliyuncs.com";
    private String sysAction = "SendSms";
    private String sysVersion = "2017-05-25";
    private MethodType sysMethod = MethodType.POST;

    @Override
    public String sendSms(String targetInfo, HashMap<String, Object> param) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GAbq622PQJxQevB3L6q", "lnKTLiDF4Ven1PjzbY3mP2IvpJqOQ8");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "15114072042");
        request.putQueryParameter("SignName", " sevenyjl");
        request.putQueryParameter("TemplateCode", "SMS_190281254");
        request.putQueryParameter("TemplateParam", "{\"code\":\"9912\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ClientException e) {
            log.error("短信发送错误：" + e.getErrMsg());
            throw e;
        }

    }
}